You write custom CUDA kernels to replace the PyTorch operators in the given EvoNorm architecture to get speedups.
You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining normalization+affine_transform+nonlinear_gating), or algorithmic changes (such as optimized memory access patterns). You are only limited by your imagination.

Technologies Used:

PyTorch: Deep learning framework

CUDA: GPU acceleration for parallel computing

C++/CUDA C++: High-performance kernel programming

Inline C++/CUDA Extension (torch.utils.cpp_extension.load_inline): Just-In-Time (JIT) compilation of custom operators

Channel Shuffle Operation: Rearranges channel dimensions for group convolution information exchange

Vectorized Memory Access (float4): Uses 128-bit wide loads/stores (4 floats) to improve memory bandwidth utilization

Grid-Stride Loops: Efficiently processes data of arbitrary size using fixed thread blocks

Memory Coalescing: Optimized memory access patterns through contiguous tensor layout

Index Calculation: Computes input/output indices using modular arithmetic for channel rearrangement

Group-based Channel Rearrangement: Implements shuffle pattern: c_out = (c_in % channels_per_group) * groups + (c_in / channels_per_group)

Type Casting Optimization: Uses reinterpret_cast for float4 vectorized memory operations

Boundary Checking: Validates tensor dimensions and divisibility constraints

Automatic Device Placement: Ensures input tensor is on CUDA device

Efficient Memory Layout: Processes spatial dimensions as vectorized units (S_vec = H*W/4)

Block Configuration: Dynamically calculates optimal block count based on problem size

Input Validation: Checks tensor dimensions, divisibility by groups, and spatial size requirements

Contiguous Memory Enforcement: Ensures input tensor is contiguous for optimal memory access

In-Place Memory Operation: Creates output tensor with same properties as input

Parallel Processing: Processes multiple samples, channels, and spatial locations concurrently

Dimension Preservation: Maintains original tensor shape while rearranging channel data

Group Convolution Support: Optimized for ShuffleNet-style architecture requirements





Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is:
import torch
import torch.nn as nn
import torch.nn.functional as F

N, C, H, W = 32, 64, 56, 56
GROUPS = 4


class ChannelShuffle(nn.Module):

    def __init__(self, groups):
        super().__init__()
        self.groups = groups

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        batch_size, num_channels, height, width = x.size()
        channels_per_group = num_channels // self.groups

        # 1. Reshape
        x = x.view(batch_size, self.groups, channels_per_group, height, width)

        # 2. Transpose (交换 groups 和 channels_per_group 维度)
        # dim 1 is groups, dim 2 is channels_per_group
        x = torch.transpose(x, 1, 2).contiguous()

        # 3. Flatten
        x = x.view(batch_size, num_channels, height, width)

        return x


class Model(nn.Module):
    def __init__(self, groups=GROUPS):
        super().__init__()
        self.op = ChannelShuffle(groups)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.op(x)


def get_inputs():
    x = torch.randn(N, C, H, W, dtype=torch.float32)
    return [x]


def get_init_inputs():
    return [GROUPS]